[PATCH] Correctly handle signed 32-bit time_t types
authorNoah Meyerhans <noahm@debian.org>
Wed, 2 Sep 2026 16:17:57 +0000 (12:17 -0400)
committerNoah Meyerhans <noahm@debian.org>
Wed, 2 Sep 2026 16:17:57 +0000 (12:17 -0400)
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1124541
Forwarded: no

dovecot handles 32-bit time_t in a couple of different ways, but neither quite
works currently.  Setting TIME_T_MAX_BITS to 31 isn't correctly handled in
places where time_t values are constructed, for example in
io_loop_get_wait_time().

Similarly, setting TIME_T_MAX_BITS = 32 and defining TIME_T_SIGNED is not
correctly handled by tm_is_too_large().

This change fixes tm_is_too_large() to set max_time to the correct maximum date
representable by a signed 32-bit time_t.

Closes: #1124541
Gbp-Pq: Name Correctly_handle_signed_32-bit_time_t_types.patch

src/lib-imap/test-imap-date.c
src/lib/time-util.c

index 22113e04076c1a5896c880b423654b42ab0441be..b5d0024e3e6cf0c961b605f8df33b74fb797daed 100644 (file)
@@ -15,7 +15,7 @@ static void test_imap_date(void)
        } tests[] = {
                { "01-Jan-1970", 0 },
                { "19-Jan-2038", 2147472000 },
-#if TIME_T_MAX_BITS >= 32
+#if TIME_T_MAX_BITS > 32
                { "07-Feb-2106", 4294944000 },
 #endif
 #if TIME_T_MAX_BITS >= 37
@@ -25,12 +25,9 @@ static void test_imap_date(void)
                { "31-Dec-9999", 253402214400LL },
 #endif
                /* conversions to maximum values */
-#if TIME_T_MAX_BITS == 31
+#if TIME_T_MAX_BITS <= 32
                { "20-Jan-2038", 2147483647 },
                { "31-Dec-9999", 2147483647 },
-#elif TIME_T_MAX_BITS == 32
-               { "08-Feb-2106", 4294967295 },
-               { "31-Dec-9999", 4294967295 },
 #endif
        };
        const char *invalid_tests[] = {
@@ -59,8 +56,10 @@ static void test_imap_datetime(void)
        } tests[] = {
                { "01-Jan-1970 00:00:00 +0000", 0, 0 },
                { "19-Jan-2038 03:14:07 +0000", 2147483647, 0 },
+#if TIME_T_MAX_BITS > 32
                { "19-Jan-2038 05:14:07 +0200", 2147483647, 2*60 },
-#if TIME_T_MAX_BITS >= 32
+#endif
+#if TIME_T_MAX_BITS > 32
                { "07-Feb-2106 06:28:15 +0000", 4294967295, 0 },
 #endif
 #if TIME_T_MAX_BITS >= 37
@@ -71,12 +70,9 @@ static void test_imap_datetime(void)
                { "31-Dec-9999 23:59:59 -2359", 253402300799LL + 23*60*60 + 59*60, -23*60 - 59 },
 #endif
                /* conversions to maximum values */
-#if TIME_T_MAX_BITS == 31
+#if TIME_T_MAX_BITS <= 32
                { "19-Jan-2038 03:14:08 +0000", 2147483647, 0 },
                { "31-Dec-9999 23:59:59 -2359", 2147483647, -23*60 - 59 },
-#elif TIME_T_MAX_BITS == 32
-               { "07-Feb-2106 06:28:16 +0000", 4294967295, 0 },
-               { "31-Dec-9999 23:59:59 -2359", 4294967295, -23*60 - 59 },
 #endif
        };
        const char *invalid_tests[] = {
index 4f89591a7e140a0c9139fe1206946a7d00ddb2e6..b035375f7c4b78c1f673794e8695e8f9c260fdae 100644 (file)
@@ -166,7 +166,7 @@ time_t time_max_safe_value(void)
 #else
        /* compute in uint64_t: with a 32-bit signed time_t
           TIME_T_MAX_BITS is 31 and (time_t)1 << 31 would overflow */
-       return (time_t)(((uint64_t)1 << TIME_T_MAX_BITS) - 1);
+       return (time_t)(((uint64_t)1 << (TIME_T_MAX_BITS - 1)) - 1);
 #endif
 }